#include "transient_decal.h"
#include "transient_backdrop.h"

#include "util/mmgr.h"
#include "util/common_macros.h"

using namespace backdrop;

TransientDecal::TransientDecal(TransientBackdrop* pBackdrop) : m_pBackdrop(pBackdrop),
	m_Layer(0),
	m_IsVisible(true)
{
}

TransientDecal::~TransientDecal()
{
}

void TransientDecal::Create(const std::string& Name, const math::Vec2& Pos, const math::Vec2& Dir)
{
	m_Name = Name;

	m_Sprite.Create(Name);
	m_Pos = m_StartPos = Pos;
	m_Dir = Dir;
}

bool TransientDecal::HitTest(const math::Vec2& Pos)
{
	if (!m_IsVisible) return false;
	return m_Sprite.IsInside(Pos);
}

bool TransientDecal::Tick(float dt, bool UpdatePos)
{
	bool bRet = m_Sprite.Tick(dt);

	if (UpdatePos && (m_Dir.MagSqr() > 0.0f))
	{
		math::Vec2 Size = m_Sprite.m_Size;

		m_Pos += dt * m_Dir;

		if (m_Pos.x > 1.0f)
			m_Pos.x = -m_Sprite.m_Size.x;
		else if ((m_Pos.x + m_Sprite.m_Size.x) < 0.0f)
			m_Pos.x = 1.0f;

		if (m_Pos.y > 1.0f)
			m_Pos.y = -m_Sprite.m_Size.y;
		else if ((m_Pos.y + m_Sprite.m_Size.y) < 0.0f)
			m_Pos.y = 1.0f;
	}

	return bRet;
}

void TransientDecal::Render()
{
	if (m_IsVisible && m_Sprite.IsValid())
	{
		m_Sprite.Setup(m_Pos, m_Sprite.m_Size);
		m_Sprite.Render();
	}
}
